package dao

import (
	"fmt"

	"gitee.com/Luna-CY/Golang-Project-Template/internal/errors"
	"gitee.com/Luna-CY/Golang-Project-Template/model"
	"gorm.io/gorm"
	"gorm.io/gorm/clause"
)

// SystemConfigurationOption SystemConfiguration 选项
type SystemConfigurationOption option

// SystemConfigurationOptionWithLock 选项：加锁查询
func SystemConfigurationOptionWithLock() SystemConfigurationOption {
	return func(session *gorm.DB, metadata *Metadata) (*gorm.DB, errors.Error) {
		return session.Clauses(clause.Locking{Strength: SelectForUpdate}), nil
	}
}

// SystemConfigurationOptionWithOrderDefault 选项：默认排序
func SystemConfigurationOptionWithOrderDefault() SystemConfigurationOption {
	return func(session *gorm.DB, metadata *Metadata) (*gorm.DB, errors.Error) {
		return session.Order(fmt.Sprintf("%s.create_time DESC", (&model.SystemConfiguration{}).TableName())), nil
	}
}

// SystemConfigurationOptionWithId 选项：根据id查询
func SystemConfigurationOptionWithId(id ...string) SystemConfigurationOption {
	return func(session *gorm.DB, metadata *Metadata) (*gorm.DB, errors.Error) {
		if 0 == len(id) {
			return nil, errors.ErrorRecordNotFound("IID_AO.SCOWI_ID.330357")
		}

		return session.Where(fmt.Sprintf("%s.id IN ?", (&model.SystemConfiguration{}).TableName()), id), nil
	}
}

func SystemConfigurationOptionWithEffectTimeLessThanOrIsZero(effectTime int64) SystemConfigurationOption {
	return func(session *gorm.DB, metadata *Metadata) (*gorm.DB, errors.Error) {
		return session.Where(fmt.Sprintf("%s.effect_time <= ?", (&model.SystemConfiguration{}).TableName()), effectTime), nil
	}
}

func SystemConfigurationOptionWithExpiredTimeGreaterThanOrIsZero(expiredTime int64) SystemConfigurationOption {
	return func(session *gorm.DB, metadata *Metadata) (*gorm.DB, errors.Error) {
		return session.Where(fmt.Sprintf("%s.expired_time >= ?", (&model.SystemConfiguration{}).TableName()), expiredTime), nil
	}
}

func SystemConfigurationOptionWithStatus(status model.SystemConfigurationStatus) SystemConfigurationOption {
	return func(session *gorm.DB, metadata *Metadata) (*gorm.DB, errors.Error) {
		return session.Where(fmt.Sprintf("%s.status = ?", (&model.SystemConfiguration{}).TableName()), status), nil
	}
}

func SystemObjectOptionWithStatus(status model.SystemObjectStatus) SystemObjectOption {
	return func(session *gorm.DB, metadata *Metadata) (*gorm.DB, errors.Error) {
		return session.Where(fmt.Sprintf("%s.status = ?", (&model.SystemObject{}).TableName()), status), nil
	}
}

func SystemObjectOptionWithCreateTimeLessThan(createTime int64) SystemObjectOption {
	return func(session *gorm.DB, metadata *Metadata) (*gorm.DB, errors.Error) {
		return session.Where(fmt.Sprintf("%s.create_time < ?", (&model.SystemObject{}).TableName()), createTime), nil
	}
}

func SystemObjectOptionWithReferCntLessThan(referCnt int64) SystemObjectOption {
	return func(session *gorm.DB, metadata *Metadata) (*gorm.DB, errors.Error) {
		return session.Where(fmt.Sprintf("%s.refer_cnt < ?", (&model.SystemObject{}).TableName()), referCnt), nil
	}
}

func SystemObjectOptionWithPlanDeleteTimeLessThan(deleteTime int64) SystemObjectOption {
	return func(session *gorm.DB, metadata *Metadata) (*gorm.DB, errors.Error) {
		return session.Where(fmt.Sprintf("%s.plan_delete_time < ?", (&model.SystemObject{}).TableName()), deleteTime), nil
	}
}
